package com.dmz.workflow.controller;

import com.dmz.workflow.req.ProcessDefinitionReq;
import com.echronos.commons.Result;
import com.echronos.commons.annotation.Permission;
import com.echronos.commons.enums.ErrorMsgEnum;
import com.dmz.workflow.constant.ExceptionMessageConstants;
import com.dmz.workflow.constant.PermissionConstants;
import com.dmz.workflow.service.ProcessDefinitionService;
import com.dmz.workflow.vo.TaskFormVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 流程定义表单、激活和挂起
 **/
@RestController
@RequestMapping("/v1/flowable/processDefinition")
@Slf4j
public class ProcessEngineController {

    @Autowired
    private ProcessDefinitionService processDefinitionService;

    /**
     * 激活流程定义  (后台流程展示 启用按钮）
     */
    @PutMapping(value = "/activate")
    @Permission(PermissionConstants.PROCESS_OPERATION_PERMISSION)
    public Result<?> activate(@RequestBody ProcessDefinitionReq actionRequest) {
        try {
            processDefinitionService.activate(actionRequest);
        } catch (Exception e) {
            return Result.build(ErrorMsgEnum.NORMAL_ERR.getErrorCode(), ExceptionMessageConstants.PROCESS_DEFINITION_BUSY_OPERATION);
        }
        return Result.build();
    }

    /**
     * 挂起流程定义 （后台流程展示 停用按钮）
     */
    @PutMapping(value = "/suspend")
    @Permission(PermissionConstants.PROCESS_OPERATION_PERMISSION)
    public Result<?> suspend(@RequestBody ProcessDefinitionReq actionRequest) {
        try {
            processDefinitionService.suspend(actionRequest);
        } catch (Exception e) {
            return Result.build(ErrorMsgEnum.NORMAL_ERR.getErrorCode(), ExceptionMessageConstants.PROCESS_DEFINITION_BUSY_OPERATION);
        }
        return Result.build();
    }

    /**
     * 发起流程中获取定义的表单和在流程中使用到的表单组件
     */
    @GetMapping(value = "/renderedStartForm")
    public Result<TaskFormVo> renderedStartForm(@RequestParam String processDefinitionId) {
        return processDefinitionService.queryTaskFormAndUsedConditions(processDefinitionId);
    }
}
